package com.xjrsoft.module.base.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.common.Enum.EnabledMarkEnum;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.dto.DesktopDto;
import com.xjrsoft.module.base.entity.XjrBaseDesktopschemeinfo;
import com.xjrsoft.module.base.mapper.XjrBaseDesktopschemeinfoMapper;
import com.xjrsoft.module.base.service.IXjrBaseDesktopschemeinfoService;
import com.xjrsoft.module.base.vo.DesktopSchemeInfoVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwq
 * @since 2021-12-01
 */
@Service
public class XjrBaseDesktopschemeinfoServiceImpl extends ServiceImpl<XjrBaseDesktopschemeinfoMapper, XjrBaseDesktopschemeinfo> implements IXjrBaseDesktopschemeinfoService {

    @Override
    public PageOutput<DesktopSchemeInfoVo> getHistory(DesktopDto desktopDto) {
        IPage<XjrBaseDesktopschemeinfo> page = this.page(ConventPage.getPage(desktopDto), Wrappers.<XjrBaseDesktopschemeinfo>query().lambda().eq(XjrBaseDesktopschemeinfo::getSchemeId, desktopDto.getSchemeId()));
        List<XjrBaseDesktopschemeinfo> xjrBaseDesktopSchemeInfoList = page.getRecords();
        List<DesktopSchemeInfoVo> desktopSchemeInfoVoList = BeanUtil.copyList(xjrBaseDesktopSchemeInfoList, DesktopSchemeInfoVo.class);
        PageOutput<DesktopSchemeInfoVo> pageOutput = new PageOutput<>();
        pageOutput.setTotal(Convert.toInt(page.getTotal()));
        pageOutput.setRows(desktopSchemeInfoVoList);
        return pageOutput;
    }

    @Override
    public boolean setNew(String id) {
        XjrBaseDesktopschemeinfo xjrBaseDesktopschemeinfo = this.getById(id);
        if(xjrBaseDesktopschemeinfo!=null) {
            String schemeId = xjrBaseDesktopschemeinfo.getSchemeId();
            XjrBaseDesktopschemeinfo one = this.getOne(Wrappers.<XjrBaseDesktopschemeinfo>query().lambda().eq(XjrBaseDesktopschemeinfo::getSchemeId, schemeId).eq(XjrBaseDesktopschemeinfo::getEnabledMark, EnabledMarkEnum.ENABLED.getCode()));
            if(one!=null) {
                one.setEnabledMark(EnabledMarkEnum.NOENABLED.getCode());
                if (this.updateById(one)) {
                    xjrBaseDesktopschemeinfo.setEnabledMark(EnabledMarkEnum.ENABLED.getCode());
                    return this.updateById(xjrBaseDesktopschemeinfo);
                }
            }
        }
        return false;
    }

}
